/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.mnt.rest

import cn.hutool.db.PageResult
import io.swagger.v3.oas.annotations.media.Schema
import jakarta.servlet.http.HttpServletResponse
import me.zhengjie.logging.annotation.Log
import me.zhengjie.modules.mnt.domain.App
import me.zhengjie.modules.mnt.service.AppService
import me.zhengjie.modules.mnt.service.dto.AppDto
import me.zhengjie.modules.mnt.service.dto.AppQueryCriteria
import org.springframework.data.domain.Pageable
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.*
import java.io.IOException

/**
 * @author zhanghouying
 * @date 2019-08-24
 */
@RestController
@Schema(description = "运维：应用管理")
@RequestMapping("/api/app")
class AppController (
    private val appService: AppService? = null
){
    @Schema(description = "导出应用数据")
    @GetMapping(value = ["/download"])
    @PreAuthorize("@el.check('app:list')")
    @Throws(
        IOException::class
    )
    fun exportApp(response: HttpServletResponse?, criteria: AppQueryCriteria?) {
        appService!!.download(appService.queryAll(criteria), response)
    }

    @Schema(description =  "查询应用")
    @GetMapping
    @PreAuthorize("@el.check('app:list')")
    fun queryApp(criteria: AppQueryCriteria?, pageable: Pageable?): ResponseEntity<PageResult<AppDto>> {
        return ResponseEntity<PageResult<AppDto>>(appService!!.queryAll(criteria, pageable) as PageResult<AppDto>, HttpStatus.OK)
    }

    @Log("新增应用")
    @Schema(description =  "新增应用")
    @PostMapping
    @PreAuthorize("@el.check('app:add')")
    fun createApp(@Validated @RequestBody resources: App): ResponseEntity<Any> {
        appService!!.create(resources)
        return ResponseEntity<Any>(HttpStatus.CREATED)
    }

    @Log("修改应用")
    @Schema(description = "修改应用")
    @PutMapping
    @PreAuthorize("@el.check('app:edit')")
    fun updateApp(@Validated @RequestBody resources: App): ResponseEntity<Any> {
        appService!!.update(resources)
        return ResponseEntity<Any>(HttpStatus.NO_CONTENT)
    }

    @Log("删除应用")
    @Schema(description = "删除应用")
    @DeleteMapping
    @PreAuthorize("@el.check('app:del')")
    fun deleteApp(@RequestBody ids: Set<Long?>): ResponseEntity<Any> {
        appService!!.delete(ids)
        return ResponseEntity<Any>(HttpStatus.OK)
    }
}